<template>
    <fullscreenModal :modal="fullscreenModal" defaultBgColor="#eee" :isAdd="false" :label="title" :isShowEdit="false"
        :showSave="false" editType="other" @handleClose="fullscreenModal = false">
        <div ref="consoleRef" class="allBox">
            <div class="listBox">
                <van-swipe class="my-swipe" indicator-color="white" ref="mySwipe">
                    <van-swipe-item v-for="(item,index) in consoleVal" :key="index">
                        <div class="swipeBox">
                            <div class="imgBox">
                                <img :src="fileUrlObj[item.fileCode] || 'images/convaccess/people.svg'"
                                    @click="handleView(fileUrlObj[item.fileCode])">
                            </div>
                            <div class="common">
                                <h5>
                                    {{item.applnt}}
                                </h5>
                                <p class="tit">就餐凭证</p>
                                <div class="ewmBox">
                                    <div class="qrcode-pic" ref=codeItem></div>
                                </div>
                            </div>
                            <div class="rowBox">
                                <div class="rowCon">
                                    <p>
                                        <label>联系电话</label>
                                        <span>{{item.applntPhone || '-'}}</span>
                                    </p>
                                    <p>
                                        <label>开始日期</label>
                                        <span>{{item.eatStartDate || '-'}}</span>
                                    </p>
                                    <p>
                                        <label>结束日期</label>
                                        <span>{{item.eatEndDate || '-'}}</span>
                                    </p>
                                </div>
                                <p class="notice">
                                    <b>就餐须知：</b>请在规定就餐时间内到指定食堂就餐，过期无效。
                                </p>
                            </div>
                        </div>
                    </van-swipe-item>
                </van-swipe>
            </div>
        </div>
    </fullscreenModal>
</template>

<script>
    import QRCode from 'qrcodejs2';
    import { getInfoById } from '@/api/srvAssurance/smarcatering/strangeEat';
    import { ImagePreview } from 'vant';
    export default {
        props: ['title'],
        data() {
            return {
                fullscreenModal: false,
                consoleVal: [],
                fileUrlObj: {}
            }
        },
        methods: {
            getData(id) {
                this.consoleVal = [];
                this.fullscreenModal = true;
                getInfoById(id).then(res => {
                    if (res.code === 1000 && res.data) {
                        let arr = [];
                        arr.push(res.data);
                        this.consoleVal=arr;
                        this.handleBatchPrintCode();
                        if (res.data.fileCode) {
                            let rr = [];
                            rr.push(res.data.fileCode);
                            this.queryFileInfoData(rr);
                        }
                    }
                })
            },
            queryFileInfoData(filecodeArr) {
                queryFileList(filecodeArr).then(res => {
                    if (res.code === 1000 && res.data) {
                        res.data.forEach(item => {
                            this.fileUrlObj[item.fileCode] = item.url;
                        })
                        this.$forceUpdate();
                    }
                })
            },
            handleBatchPrintCode() {
                //必须要等到页面加载完成，不然会报错
                setTimeout(() => {
                    this.consoleVal.forEach((item, index) => {
                        let code = item.eatCode;
                        this.$refs.codeItem[index].innerHTML = "";
                        new QRCode(this.$refs.codeItem[index], {
                            text: code, //二维码内容
                            width: 150,
                            height: 150,
                        })
                    })
                }, 200)
            },
            // 图片预览
            handleView(url) {
                if (url) {
                    ImagePreview({
                        images: [url],
                        closeable: true,
                    });
                }
            }
        }
    }
</script>

<style scoped lang='less'>
    .allBox {
        margin: 0 auto;
        width: 100%;

        .listBox {
            position: relative;
            padding: 12px;
            color: #333;

            .changeBtn {
                position: absolute;
                z-index: 99;
                width: 30px;
                height: 30px;
                text-align: center;
                line-height: 30px;
                background: #ccc;
                border-radius: 50%;
                top: 50%;
                transform: translateY(-50%);

                .van-icon {
                    color: #fff;
                }
            }

            .preBtn {
                left: 5px;
            }

            .nextBtn {
                right: 5px;
            }

            .swipeBox {
                margin-top: 35px;
                position: relative;
                width: 100%;
                box-sizing: border-box;
            }

            .ewmBox {
                width: 100%;
                display: flex;
                justify-content: center;
                align-items: center;

                &>div {
                    border: 8px solid #e8eef1;
                    padding: 15px;
                    width: 150px;
                    height: 150px;
                    margin: 5px 0;
                    border-radius: 5px;
                }
            }

            .imgBox {
                width: 70px;
                height: 70px;
                border-radius: 50%;
                padding: 2px;
                box-sizing: border-box;
                position: absolute;
                top: -35px;
                left: 50%;
                transform: translateX(-50%);
                background: #fff;
                overflow: hidden;

                img {
                    width: 100%;
                    height: 100%;
                    border-radius: 50%;
                }
            }

            .common {
                padding: 35px 10px 10px;
                box-sizing: border-box;
                border-radius: 5px;
                line-height: 25px;
                text-align: center;
                background: #fff;

                h5 {
                    font-size: 15px;

                    .type {
                        background: #f2a84e;
                        font-size: 9px;
                        color: #fff;
                        margin-left: 3px;
                        padding: 2px 3px;
                        border-radius: 3px;
                    }

                    .reason {
                        background: #4eb3e9;
                    }
                }

                .tip {
                    color: #888;
                    font-size: 12px;
                    line-height: 20px;
                }

                .tit {
                    font-weight: bold;
                    font-size: 12px;
                    margin-top: 10px;
                }
            }

            .rowBox {
                background: #fff;
                margin-top: 10px;
                padding: 10px;
                box-sizing: border-box;
                border-radius: 5px;

                .notice {
                    margin-top: 5px;
                    line-height: 25px;
                    text-align: left;
                }

                .rowCon p {
                    display: flex;
                    line-height: 28px;
                    text-align: left;

                    label {
                        width: 75px;
                        color: #999;
                    }

                    span {
                        flex: 1;
                        overflow: hidden;
                        white-space: nowrap;
                        text-overflow: ellipsis;
                    }
                }
            }

            .custom-indicator {
                margin-top: 15px;
                text-align: center;
            }
        }
    }
</style>